
/********************************************************************************
** This file contains the definition of OpenQ10 MainWindow
**
** Created: Thu 29. Nov 10:52:01 2007
**      by: Imran Shahid
**
********************************************************************************/

#include "mainwindow.h"
#include "textedit.h"
#include "infobar.h"

#include <QVariant>
#include <QAction>
#include <QApplication>
#include <QButtonGroup>
#include <QCloseEvent>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QMessageBox>
#include <QStatusBar>
#include <QTextCodec>
#include <QTextStream>
#include <QWidget>

MainWindow::MainWindow()
{
    appname = QString::fromUtf8("OpenQ10");

    setupActions();
    setupUi();

    textcolor = new QColor(208, 160, 0, 255);               // default text color
    backgroundcolor = new QColor(0, 0, 0, 255);             // default background color
    font = new QFont(QString::fromUtf8("Courier New"), 10); // default font

    this->backgroundColorChanged(backgroundcolor);

    textedit = new TextEdit(centralwidget);
    textedit->backgroundColorChanged(backgroundcolor);
    textedit->textColorChanged(textcolor);
    textedit->fontChanged(font);

    infobar->setPaletteColor();

    // set textedit bounds here
    QDesktopWidget *desktop = QApplication::desktop();
    QRect sg = desktop->screenGeometry(desktop->primaryScreen());
    centralwidget->resize(sg.width(), sg.height());
    textedit->resize(centralwidget->width()-100, centralwidget->height()-infobar->height());
    textedit->move(50, 0);

    setFileName(QString());
    textedit->setFocus();

    const QStringList args = QCoreApplication::arguments();
    if (args.count() == 2)
        filename = args.at(1);

    if (!load(filename))
        fileNew();
} // MainWindow

void MainWindow::backgroundColorChanged(QColor *color)
{
    QPalette palette;
    QBrush brush(QColor(255, 255, 255, 255));
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::WindowText, brush);
    QBrush brush1(color->rgb());
    brush1.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Button, brush1);
    palette.setBrush(QPalette::Active, QPalette::Light, brush1);
    palette.setBrush(QPalette::Active, QPalette::Midlight, brush1);
    palette.setBrush(QPalette::Active, QPalette::Dark, brush1);
    palette.setBrush(QPalette::Active, QPalette::Mid, brush1);
    palette.setBrush(QPalette::Active, QPalette::Text, brush);
    palette.setBrush(QPalette::Active, QPalette::BrightText, brush);
    palette.setBrush(QPalette::Active, QPalette::ButtonText, brush);
    palette.setBrush(QPalette::Active, QPalette::Base, brush1);
    palette.setBrush(QPalette::Active, QPalette::Window, brush1);
    palette.setBrush(QPalette::Active, QPalette::Shadow, brush1);
    palette.setBrush(QPalette::Active, QPalette::AlternateBase, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::WindowText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Button, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::Light, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::Midlight, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::Dark, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::Mid, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::Text, brush);
    palette.setBrush(QPalette::Inactive, QPalette::BrightText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::ButtonText, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Base, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::Window, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::Shadow, brush1);
    palette.setBrush(QPalette::Inactive, QPalette::AlternateBase, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Button, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Light, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Midlight, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Dark, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Mid, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Text, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::BrightText, brush);
    palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Base, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Window, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::Shadow, brush1);
    palette.setBrush(QPalette::Disabled, QPalette::AlternateBase, brush1);
    this->setPalette(palette);
} // backgroundColorChanged

void MainWindow::setupUi()
{
    if (this->objectName().isEmpty())
        this->setObjectName(QString::fromUtf8("MainWindow"));
    this->setWindowModality(Qt::ApplicationModal);
    this->setWindowIcon(QIcon(QString::fromUtf8(":/icons/openq10.png")));
    this->setAutoFillBackground(true);
    centralwidget = new QWidget(this);
    centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
    this->setCentralWidget(centralwidget);

    infobar = new InfoBar(this);
    infobar->setObjectName(QString::fromUtf8("infobar"));
    this->setStatusBar(infobar);

    retranslateUi();

    QMetaObject::connectSlotsByName(this);
} // setupUi

void MainWindow::retranslateUi()
{
    this->setWindowTitle(appname);
    this->setStyleSheet(QString());
    Q_UNUSED(this);
} // retranslateUi

void MainWindow::closeEvent(QCloseEvent *e)
{
    if (maybeSave())
        e->accept();
    else
        e->ignore();
} // closeEvent

void MainWindow::setupActions()
{
//    setWindowModified(textedit->document()->isModified());
//    connect(textedit->document(), SIGNAL(modificationChanged(bool)),
//            this, SLOT(setWindowModified(bool)));

//    actionSave->setEnabled(textedit->document()->isModified());

//    connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
} // setupActions

bool MainWindow::load(const QString &fn)
{
    if (!QFile::exists(fn))
        return false;
    QFile file(fn);
    if (!file.open(QFile::ReadOnly))
        return false;

    QByteArray data = file.readAll();
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QString str = codec->toUnicode(data);
    str = QString::fromLocal8Bit(data);
    textedit->setPlainText(str);

    setFileName(fn);
    return true;
} // load

bool MainWindow::maybeSave()
{
    if (!textedit->document()->isModified())
        return true;
    if (filename.startsWith(QLatin1String(":/")))
        return true;
    QMessageBox::StandardButton ret;
    ret = QMessageBox::warning(this, appname,
                               tr("The document has been modified.\n"
                                  "Do you want to save your changes?"),
                               QMessageBox::Save | QMessageBox::Discard
                               | QMessageBox::Cancel);
    if (ret == QMessageBox::Save)
        return fileSave();
    else if (ret == QMessageBox::Cancel)
        return false;
    return true;
} // maybeSave

void MainWindow::setFileName(const QString &fn)
{
    this->filename = fn;
    textedit->document()->setModified(false);

    QString shownName;
    if (filename.isEmpty())
        shownName = "untitled.txt";
    else
        shownName = QFileInfo(filename).fileName();

    setWindowTitle(tr("%1 - %2[*]").arg(appname).arg(shownName));
    setWindowModified(false);
} // setFileName

void MainWindow::fileNew()
{
    if (maybeSave()) {
        textedit->clear();
        setFileName(QString());
    }
} // fileNew

void MainWindow::fileOpen()
{
    QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
                                              QString(), tr("Text Files (*.txt);;All Files (*.*)"));
    if (!fn.isEmpty())
        load(fn);
} // fileOpen

bool MainWindow::fileSave()
{
    if (filename.isEmpty())
        return fileSaveAs();

    QFile file(filename);
    if (!file.open(QFile::WriteOnly))
        return false;
    QTextStream ts(&file);
    ts.setCodec(QTextCodec::codecForName("UTF-8"));
    ts << textedit->document()->toPlainText();
    textedit->document()->setModified(false);
    return true;
} // fileSave

bool MainWindow::fileSaveAs()
{
    QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
                                              QString(), tr("Text Files (*.txt);;All Files (*.*)"));
    if (fn.isEmpty())
        return false;
    setFileName(fn);
    return fileSave();
} // fileSaveAs

void MainWindow::help()
{
    QMessageBox::about(this, tr("About"), tr("This example demonstrates Qt's "
                                             "rich text editing facilities in action, providing an example "
                                             "document for you to experiment with."));
} // help

